//
//  UIBarButtonItem+Extension.swift
//  SJWeiboSwift
//
//  Created by app;e on 15/10/4.
//  Copyright © 2015年 world. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    convenience init(imageName:String,hlightedImageName:String?) {
        let btn = UIButton()
        //??表示如果前面的内容为nil, 则使用后面的内容
        let hImageName = hlightedImageName ?? imageName+"_highlighted"
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: hImageName), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        self.init(customView: btn)
    }
    
    convenience init(imageName:String,hlightedImageName:String?,trag:AnyObject?,actionName:String?) {
        let btn = UIButton()
        let hImageName = hlightedImageName ?? imageName+"_highlighted"
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: hImageName), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        if actionName != nil {
            btn.addTarget(trag, action: Selector(actionName!), forControlEvents: UIControlEvents.TouchUpInside)
        }
        self.init(customView: btn)
    }

}
